package com.nit.config;

import com.nit.utils.JWTUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getMethod().equals("GET") || request.getRequestURL().indexOf("/api/system") != -1 ) {
            response.setHeader("tokenstatus", "ok");
            return true;
        } else {
            if (JWTUtil.verify(request.getHeader("token"), request.getHeader("userId"))) {
                response.setHeader("tokenstatus", "ok");
                return true;
            } else {
                response.setHeader("tokenstatus", "error");
                return false;
            }
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
